package com.example.textcompare.demos.action;

import com.spire.doc.Document;

import java.io.FileWriter;
import java.io.IOException;

/**
 *  本类用于测试Word文档转为->txt文件
 *  存在问题：生成文件首行出现出现：
 *  Evaluation Warning: The document was created with Spire.Doc for JAVA.
 */
public class ExtractText {

    public static void main(String[] args) throws IOException {

        //加载Word文档
        Document document = new Document();
        document.loadFromFile("D:\\AAAA\\1.docx");

        //获取文档中的文本保存为String
        String text=document.getText();

        //将String写入Txt文件
        writeStringToTxt(text,"D:\\ExtractedText.txt");
    }

    public static void writeStringToTxt(String content, String txtFileName) throws IOException {

        FileWriter fWriter= new FileWriter(txtFileName,true);
        try {
            fWriter.write(content);
        }catch(IOException ex){
            ex.printStackTrace();
        }finally{
            try{
                fWriter.flush();
                fWriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}